﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MidApp
{
    public class BussinessObject
    {
        public static bool Login(string nick, string password)
        {
            try
            {
                var user = MidApp.BussinessObject.GetUserByNick(nick);

                if (MidApp.Hasher.PasswordHasher(password) == user.pass.Trim())
                {
                    return true;
                }
                else
                {
                    return false;
                }           
            }
            catch (Exception f)
            {
                return false;
            }
        }

        public static void Registration(string nick, string password)
        {
            AppLib.log newevent = new AppLib.log();

            newevent.nick = nick;
            newevent.pass = MidApp.Hasher.PasswordHasher(password);

            var dbConn = MidApp.LinqUse.DBConn;
            dbConn.logs.InsertOnSubmit(newevent);
            dbConn.SubmitChanges();
        }

        public static void Editation(string oldPassword, string newPassword)
        {
            var db = MidApp.LinqUse.DBConn;

            var user = (from log in db.logs where log.pass == oldPassword select log).First();
            user.pass = MidApp.Hasher.PasswordHasher(newPassword);
            db.SubmitChanges();
        }

        public static AppLib.log GetUserByNick(string nick)
        {
            var db = MidApp.LinqUse.DBConn;

            return (from log in db.logs where log.nick == nick select log).First();
        }

        public static AppLib.log GetUserByPass(string pass)
        {
            var db = MidApp.LinqUse.DBConn;

            return (from log in db.logs where log.pass == pass select log).First();
        }
    }
}
